package com.disv.service.resp;

import com.alibaba.fastjson2.JSON;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.Collections;
import java.util.List;

/**
 * @desc: ReturnResp
 * @author: Chao Zhang
 * @date: 2022/10/28 17:56
 */
public interface ReturnResp {

    String getCode();

    String getMessage();

    String getModule();

    default <T> Resp<T> resp() {
        return resp(null);
    }

    default <T> Resp<T> resp(T data, Object... args) {
        return Resp.<T>builder().module(getModule()).code(getCode()).msg(MessageFormat.format(getMessage(), args)).data(data).build();
    }

    default <T> PageResp<T> pageResp() {
        return pageResp(Collections.emptyList(), false, 0, 0);
    }

    default <T> PageResp<T> pageResp(List<T> data, boolean hasNext, long totalPage, long totalCount, Object... args) {
        return PageResp.<T>builder().module(getModule()).code(getCode()).msg(MessageFormat.format(getMessage(), args)).data(data)
                .hasNext(hasNext).totalPage(totalPage).totalCount(totalCount).build();
    }


    default void printAsJson(PrintWriter writer) {
        writer.print(JSON.toJSONString(resp()));
        writer.flush();
    }

}
